package com.xy.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Admin;
import com.xy.model.PayMent;
import com.xy.service.PayMentService;
import com.xy.service.impl.PayMentServiceImpl;

/**
 * Servlet implementation class PayMentServlet
 */
@WebServlet("/payMentServlet")
public class PayMentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PayMentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		PayMentService service = new PayMentServiceImpl();

		if ("go".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			PageResults pageResults = new PayMentServiceImpl().listByStatus(currentPage);

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "go");
			request.setAttribute("list", null);

			request.getRequestDispatcher("/pages/system/system001/order_list_name.jsp").forward(request, response);
		} else if ("list".equals(request.getParameter("status"))) {
			int currentPage = 0;
			PageResults pageResults = null;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			if ("query".equals(request.getParameter("pre"))) {
				
				Integer flag = Integer.valueOf(request.getParameter("flag"));
				String hql = "";
				String countHql = "";
				String supplier = request.getParameter("supplier") == null ? "%" : request.getParameter("supplier");
				
				switch (flag) {
				case 0:
					hql = "from PayMent where (status!=1 or status=null) and stockReceive.supplier.name like '%" + supplier + "%'";
					countHql = "select count(*) from PayMent where (status!=1 or status=null) and stockReceive.supplier.name like '%" + supplier + "%'";
					break;
				case 1:
					hql = "from PayMent where status=1 and stockReceive.supplier.name like '%" + supplier + "%'";
					countHql = "select count(*) from PayMent where status=1 and stockReceive.supplier.name like '%" + supplier + "%'";
					break;
				case 2:
					hql = "from PayMent where stockReceive.supplier.name like '%" + supplier + "%'";
					countHql = "select count(*) from PayMent where stockReceive.supplier.name like '%" + supplier + "%'";
					break;
				default:
					break;
				}
				pageResults = new PayMentServiceImpl().listByHql(hql,countHql,currentPage);
			} else {
				pageResults = new PayMentServiceImpl().listByPage(currentPage);
			}

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "list");
			request.setAttribute("list", null);

			request.getRequestDispatcher("/pages/system/system001/order_list_name2.jsp").forward(request, response);
		} else if ("query".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			String supplier = null;
			String list = null;

			if (request.getParameter("list") == null) {
				supplier = request.getParameter("supplier") == null ? "%" : request.getParameter("supplier");
				list = supplier;
			} else {
				supplier = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
				list = supplier;
			}

			PageResults pageResults = service.query(supplier, currentPage);

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "query");
			request.setAttribute("list", list);

			request.getRequestDispatcher("/pages/system/system001/order_list_name.jsp").forward(request, response);
		} else if ("pay".equals(request.getParameter("status"))) {
			String[] s = request.getParameterValues("checkbox");
			// List<PayMent> payMents = new ArrayList();
			for (int i = 0; i < s.length; i++) {
				PayMent payMent = (PayMent) service.get(Integer.valueOf(s[i]));
				// payMents.add(payMent);
				payMent.setPayDate(new Date());
				payMent.setAdmin((Admin) request.getSession().getAttribute("admin"));
				payMent.setStatus("1");
				service.update(payMent);
			}
			request.getRequestDispatcher("/payMentServlet?status=go&currentPage=0").forward(request, response);
		}
	}

}
